package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.qurey.TravelQuery;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("travel")
public class TravelController {

    @Autowired
    private ITravelService travelService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")TravelQuery qo){
        //PageInfo  : 分页数据封装对象, Mybatis插件, 操作的是Mysql数据库
        //Page : 分页数据封装对象, spring-data 的组件, 操作是mongodb
        Page<Travel> page = travelService.query(qo);
        model.addAttribute("page", page);

        return "travel/list";
    }




    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Travel travel){

        travelService.saveOrUpdate(travel);

        return JsonResult.success();
    }



    //@RequestMapping("/input")
    //public String input(Model model, String id){
    //
    //    if(StringUtils.hasLength(id)){
    //        //travel
    //        model.addAttribute("travel", travelService.get(id));
    //    }
    //
    //
    //    return "travel/input";
    //}
    //
    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(String id, int state){


        return JsonResult.success();
    }

    @RequestMapping("/audit")
    @ResponseBody
    public Object audit(String id, int state){


        travelService.audit(id, state);

        return JsonResult.success();
    }


    @RequestMapping("/getContentById")
    @ResponseBody
    public Object getContentById(String id){


        Travel travel = travelService.get(id);

        return JsonResult.success(travel.getContent());
    }


    @RequestMapping("/updateState")
    @ResponseBody
    public Object updateState(String id,String state){

        travelService.updateState(id,state);

        return JsonResult.success();
    }
}
